﻿using System;

namespace _115_枚举转换测试
{
    class Program
    {
        static void Main(string[] args)
        {
            //1-从枚举 转换为 字符串
            Console.WriteLine("1-从枚举 转换为 字符串");
            Console.WriteLine(RequestCode.Login.ToString());
            string enumStr = Enum.GetName(typeof(RequestCode), RequestCode.Login);
            Console.WriteLine(enumStr);
            //2-从枚举 转换为 int
            Console.WriteLine("2-从枚举 转换为 int");
            Console.WriteLine(((int)RequestCode.Login).ToString()); //枚举默认从0开始
            //3-从字符串 转换为 枚举
            Console.WriteLine("3-从字符串 转换为 枚举");
            if(Enum.TryParse("Login", out RequestCode str2enum))
            {
                Console.WriteLine("Login转换为枚举：" + str2enum);
            }
            //4-从int 转换为 枚举
            Console.WriteLine("4-从int 转换为 枚举");
            RequestCode curEnum = (RequestCode)Enum.ToObject(typeof(RequestCode), 0);
            Console.WriteLine(curEnum.ToString());
            RequestCode _0Enum = (RequestCode)0;
            Console.WriteLine("直接使用枚举类强转int成枚举：" + _0Enum.ToString());

            //5-访问枚举所有的内容，并打印成字符串
            Console.WriteLine("5-访问枚举所有的内容，并打印成字符串");
            Array array = Enum.GetValues(typeof(RequestCode));
            foreach (RequestCode item in array)
            {
                Console.WriteLine(item);
            }


            Console.ReadKey();
        }
    }
}
